<!--#include file="functions.asp" -->
<%

'set cn=Server.CreateObject("ADODB.Connection")
'cn.Open "MP2ENTTEST2", "MP2", "MP2"
dim sql

if (request("seqnum2")="0") then
  dim InvRegNo  
  sql = "SELECT REGPREFIX, REGNUM FROM EXJRNLPREF"
  set RS=cn.execute(sql)
  InvRegNo = rs("REGPREFIX") & String(4-Len(CStr(rs("REGNUM"))),"0") & CStr(rs("REGNUM")) 
  set RS=nothing
  'sql = "UPDATE EXJRNLPREF SET REGNUM=REGNUM+1"
  'cn.execute(sql)
	
  sql = "INSERT INTO dbo.EXIREG ("
  sql = sql & "REGNUM, INVNUM, REFORDER, VENDORID, VENDORBRANCHID, INVRECDATE, INVDUEDATE, EMPCODE, DEPARTMENT,"
  sql = sql & "PONUM, INVCURRENCY, CURRENCYSYMBOL, INVAMOUNT, COMPCODE, COMMENTS, "
  sql = sql & "USERCOORD, VERIFIEDBY, LOCATION, INVSTATUS, UPDATEUSER, UPDATESTAMP "
  if (request("RefOrder")="N") then
  	sql = sql & ",DOC1, DOC2"
  end if
  sql = sql & ") VALUES ('"
  sql = sql & InvRegNo & "','"
  sql = sql & request("reqInvoiceNo") & "','"
  if (request("RefOrder")="Y") then
  	sql = sql &  "Y','"
  end if
  if (request("RefOrder")="N") then 
  	sql = sql &  "N','"
  end if
  if (request("RefOrder")="O") then 
  	sql = sql &  "O','"
  end if
  sql = sql & request("reqVendorID") & "','"
  sql = sql & request("reqBranch") & "','"
  sql = sql & request("ReceivedDate") & "','"
  sql = sql & request("DueDate") & "','"
  sql = sql & request("reqReceivedBy") & "','"
  sql = sql & request("Dept") & "','"
  sql = sql & request("reqPONum") & "','"
  sql = sql & request("reqCurrency") & "','"
  sql = sql & request("CurSymbol2") & "',"
  if (request("reqInvoiceAmount")<>"") then
  	sql = sql & delkoma(request("reqInvoiceAmount")) & ",'"
  else
  	sql = sql &  "0,'"
  end if
  
  sql = sql & request("CompCode") & "','"
  sql = sql & "Vendor Notes : " & Chr(13) & request("reqDescription") & "','"
  
  sql = sql & request("reqUserCoord") & "','"
  sql = sql & request("reqUserDept") & "','"
  sql = sql & request("reqUserCoord") & "','"
  sql = sql & "ROUTED','"
  sql = sql & request("reqReceivedBy") & "',"
  sql = sql & "GETDATE()"
  
  sql = sql & ")" 
  cn.execute(sql)
  
  sql = "UPDATE EXJRNLPREF SET REGNUM=REGNUM+1"
  cn.execute(sql)
  
else
  sql = "UPDATE EXIREG SET "
  sql = sql & "INVNUM='" & request("reqInvoiceNo") & "',"
  sql = sql & "VENDORID='" & request("reqVendorID") & "',"
  sql = sql & "VENDORBRANCHID='" & request("reqBranch") & "',"
  sql = sql & "INVRECDATE='" & request("ReceivedDate") & "',"
  sql = sql & "INVDUEDATE='" & request("DueDate") & "',"
  sql = sql & "EMPCODE='" & request("reqReceivedBy") & "',"
  sql = sql & "PONUM='" & request("reqPONum") & "',"
  sql = sql & "DEPARTMENT='" & request("Dept") & "',"
  sql = sql & "INVCURRENCY='" & request("reqCurrency") & "',"
  sql = sql & "CURRENCYSYMBOL='" & request("CurSymbol2") & "',"
  if (request("Comments")<>"") then
    txComments = dblq(request("Comments1")) & Chr(13)
	txComments = txComments & ">" & UCase(UsrFullname) & "(" & now &"):  " 
	txComments = txComments & dblq(request("Comments"))
	sql = sql & " COMMENTS= '" & txComments & "',"
  end if
  sql = sql & "INVAMOUNT=" & delkoma(request("reqInvoiceAmount")) & ","
  sql = sql & "COMPCODE='" & request("CompCode") & "',"
  if (request("RefOrder")="Y") then
  sql = sql &  "REFORDER='Y',"
  end if
  if (request("RefOrder")="N") then 
  sql = sql &  "REFORDER='N',"
  end if
  if (request("RefOrder")="O") then 
  sql = sql &  "REFORDER='O',"
  end if
  if (request("cbVAT")="Y") then
  sql = sql & "VATPAIDTOVENDOR='Y',"
  else
  sql = sql & "VATPAIDTOVENDOR='N',"
  end if
  sql = sql & "WHTNAME='" & request("WHTName") & "',"
  sql = sql & "WHTRATE=" & request("WHTRate") & ","
  sql = sql & "VATRATE=" & request("VATRate") & ","
  sql = sql & "WHTAMOUNT=" & delkoma(request("WHTAmt")) 
  if (request("RefOrder")<>"Y") then
    if (request("cbOri")="Y") then
  	sql = sql & ",DOC1='Y',"
  	else
  	sql = sql & ",DOC1='N',"
  	end if
  	if (request("cbTax")="Y") then
  	sql = sql & "DOC2='Y'"
  	else
  	sql = sql & "DOC2='N'"
  	end if
  end if
  sql = sql & " WHERE SEQNUM =" & request("seqnum2") 
  cn.execute(sql)
end if
'cn.execute(sql)

'update exicharge
'if (request("RefOrder") ="Y") then
    For i = 1 to request("Incr")-1
	if (request("aseqnum"&i) <> "0") then
	  sql = "UPDATE EXICHARGE SET "
	  sql = sql & "ACCTCODE='" & request("ActCode"&i) & "',"
	  sql = sql & "T0CODE='" & request("T0"&i) & "',"
	  sql = sql & "T1CODE='" & request("T1"&i) & "',"
	  sql = sql & "T2CODE='" & request("T2"&i) & "',"
	  sql = sql & "T3CODE='" & request("T3"&i) & "',"
	  sql = sql & "AMOUNT=" & delkoma(request("Amt"&i)) & " "
	  sql = sql & " WHERE SEQNUM=" & request("aseqnum"&i)
	  cn.execute(sql)
	else
	  if (request("ActCode"&i) <> "") then
	    sql = "INSERT INTO EXICHARGE(INVSEQNUM,ACCTCODE,T0CODE,"
	  	sql = sql & "T1CODE, T2CODE, T3CODE, AMOUNT) VALUES ("
	  	sql = sql & request("seqnum2") & ",'"
	  	sql = sql & request("ActCode"&i) & "','"
	  	sql = sql & request("T0"&i) & "','"
	  	sql = sql & request("T1"&i) & "','"
	  	sql = sql & request("T2"&i) & "','"
	  	sql = sql & request("T3"&i) & "',"
	  	sql = sql & delkoma(request("Amt"&i)) & ")"
	  cn.execute(sql)
	  end if
	end if
	
	Next
'end if

'Update Receipt
For i = 1 to CInt(request("Incr3"))-1
	  if (request("cb"&i)="Y")and(request("isEdit"&i)="Y") then
	    sql = "INSERT INTO EXILINKRCP (SEQNUM, PONUM, RECEIVERNUM)"
	    sql = sql & " VALUES ("
	    sql = sql & request("seqnum2") & ",'"
	    sql = sql & request("reqPONUM") & "','"
	    sql = sql & request("rn"&i) & "')"
	    cn.execute(sql)

	    sql = "EXEC dbo.EXIPORECUPDATE "
	  	sql = sql & "@InvoiceStatus='" & request("status") & "',"
	  	sql = sql & "@InvoiceNum='" & request("reqInvoiceNo") & "',"
	  	sql = sql & "@InvoiceDate='" & request("ReceivedDate") & "',"
	  	sql = sql & "@PONumber='" & request("reqPONum") & "',"
	  	sql = sql & "@ReceiverNum='" & request("rn"&i) & "'"
	  
	  	cn.execute(sql)
		isUpdate="Y"
	  End if
	Next
	
	if (isUpdate="Y") then
	sql = "UPDATE EXIREG SET INVSTATUS='APPROVED',"
	sql = sql & "SERVICERCP='Y', "
	sql = sql & "LOCATION='FINANCE' "
	sql = sql & " WHERE SEQNUM=" & request("seqnum2")
	cn.execute(sql)
	
	sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  	sql = sql & " VALUES ("
  	sql = sql & request("seqnum2") & ",'"
	sql = sql & "IUR','"
 	sql = sql & "FINANCE','"
  	sql = sql & request("Dept") & "','"
  	sql = sql & request("RouteDate") & "','"
  	sql = sql & usrName & "',"
  	sql = sql & "GETDATE() )"
    cn.execute(sql)	
	end if
'end of update receipt

dim tmpNum
if (request("seqnum2") = "0") then
  sql = "SELECT MAX(SEQNUM) FROM EXIREG"
  set rs=cn.execute(sql)
  tmpNum=rs(0)
  sql = "INSERT INTO EXIHIST (SEQNUM, TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  sql = sql & " VALUES ("
  sql = sql & tmpNum & ",'"
  sql = sql & "IRC','"
  sql = sql & request("reqReceivedBy") & "','"
  sql = sql & request("Dept") & "','"
  sql = sql & request("ReceivedDate") & "','"
  sql = sql & usrName & "',"
  sql = sql & "GETDATE() )"
  
  cn.execute(sql)
  
  response.redirect "inv_Print.asp?seqnum=" &tmpNum
 
else
 tmpNum = request("seqnum2")
 'response.redirect "Inv_Detail.asp?seqnum=" & tmpNum
 response.redirect "Inv_Detail.asp?seqnum="& tmpNum &"&InvStatus="& request("InvStatus")&"&dsort="& request("dsort")&"&dorder="&request("dorder") &"&Location="&request("Location")&"&UserCoord="&request("UserCoord")&"&RefOrder="&request("RefOrder2")&"&VendorID="&request("VendorID")&"&CurSymbol="& request("CurSymbol")
 
end if 
%>
